package cn.iocoder.yudao.module.knowledge.controller.admin.tree;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.knowledge.controller.admin.tree.vo.*;
import cn.iocoder.yudao.module.knowledge.dal.dataobject.tree.KnowledgeTreeDO;
import cn.iocoder.yudao.module.knowledge.service.tree.KnowledgeTreeService;

@Tag(name = "管理后台 - 知识库结构树")
@RestController
@RequestMapping("/knowledge/tree")
@Validated
public class KnowledgeTreeController {

    public KnowledgeTreeController() {
        System.out.println("知识库结构树后台加载");
    }

    @Resource
    private KnowledgeTreeService treeService;

    @PostMapping("/create")
    @Operation(summary = "创建知识库结构树")
    @PreAuthorize("@ss.hasPermission('knowledge:tree:create')")
    public CommonResult<Long> createTree(@Valid @RequestBody KnowledgeTreeSaveReqVO createReqVO) {
        return success(treeService.createTree(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新知识库结构树")
    @PreAuthorize("@ss.hasPermission('knowledge:tree:update')")
    public CommonResult<Boolean> updateTree(@Valid @RequestBody KnowledgeTreeSaveReqVO updateReqVO) {
        treeService.updateTree(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除知识库结构树")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('knowledge:tree:delete')")
    public CommonResult<Boolean> deleteTree(@RequestParam("id") Long id) {
        treeService.deleteTree(id);
        return success(true);
    }


    @GetMapping("/get")
    @Operation(summary = "获得知识库结构树")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    // 知识库开放给所有人 不需要权限
//    @PreAuthorize("@ss.hasPermission('knowledge:tree:query')")
    public CommonResult<KnowledgeTreeRespVO> getTree(@RequestParam("id") Long id) {
        KnowledgeTreeDO tree = treeService.getTree(id);
        return success(BeanUtils.toBean(tree, KnowledgeTreeRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得知识库结构树列表")
    // 知识库开放给所有人 不需要权限
//    @PreAuthorize("@ss.hasPermission('knowledge:tree:query')")
    public CommonResult<List<KnowledgeTreeRespVO>> getTreeList(@Valid KnowledgeTreeListReqVO listReqVO) {
        List<KnowledgeTreeDO> list = treeService.getTreeList(listReqVO);
        return success(BeanUtils.toBean(list, KnowledgeTreeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出知识库结构树 Excel")
    @PreAuthorize("@ss.hasPermission('knowledge:tree:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTreeExcel(@Valid KnowledgeTreeListReqVO listReqVO,
                                HttpServletResponse response) throws IOException {
        List<KnowledgeTreeDO> list = treeService.getTreeList(listReqVO);
        // 导出 Excel
        ExcelUtils.write(response, "知识库结构树.xls", "数据", KnowledgeTreeRespVO.class,
                BeanUtils.toBean(list, KnowledgeTreeRespVO.class));
    }

}
